package slash.util;

import java.util.ArrayList;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Input;
import org.newdawn.slick.util.Log;

import slash.MainGame;
import slash.world.*;
import slash.collision.CollisionMask;
import slash.constants.Globals;
import slash.entities.AbstractEntity;
import slash.entities.LivingEntity;

/**
 * A class that binds an entity to the controller
 * @author Julien Gilbert
 *
 */
public class ControlHandler {
	
	private LivingEntity owner;
	private boolean enabled = false;
	
	public ControlHandler(LivingEntity owner){
		this.owner = owner;
	}
	
	/**
	 * Processes Input and moves entity
	 * @param i
	 * @param time
	 */
	public void handleInput(Input i, float time){
		if(!enabled)return;
		
		float entityX = owner.getPosX();// original X position of entity
		float entityY = owner.getPosY();// original Y position of entity
		float speed = owner.getSpeed();// speed of entity
		
		if(i.isKeyDown(Input.KEY_DOWN)) {
			/* sets entity animation to direction matching the input */
            owner.setCurrentAnimation(Globals.ANIMATION_DOWN);
            owner.getCurrentAnimation().start();
            /* moves entity in direction based on speed*/
            owner.setPosY(entityY + speed * time);
            /* checks for collision, if so, move entity back to original location */
            /*if(checkCollision())
            	owner.setPosY(entityY);*/
        }
        if(i.isKeyDown(Input.KEY_UP)) {
            owner.setCurrentAnimation(Globals.ANIMATION_UP);
            owner.getCurrentAnimation().start();
            owner.setPosY(entityY - speed * time);
           /* if(checkCollision())
            	owner.setPosY(entityY);*/
        }
        if(i.isKeyDown(Input.KEY_LEFT)) {
            owner.setCurrentAnimation(Globals.ANIMATION_LEFT);
            owner.getCurrentAnimation().start();
            owner.setPosX(entityX - speed * time);
   /*         if(checkCollision())
            	owner.setPosX(entityX);*/
        }
        if(i.isKeyDown(Input.KEY_RIGHT)) {
            owner.setCurrentAnimation(Globals.ANIMATION_RIGHT);
            owner.getCurrentAnimation().start();
            owner.setPosX(entityX + speed * time);
/*            if(checkCollision())
            	owner.setPosX(entityX);*/
        }
        
        /* debug prints if an input was pressed */
        if(i != null && MainGame.debug()){
        	//Log.debug("An input has been pressed!");
        }
	}
	
	/**
	 * Checks to see if the entity will collide
	 * if it moves. Returns true if an entity
	 * will collide with this entity.
	 * @return
	 */
	public boolean checkCollision(){
		ArrayList<AbstractEntity> list = MainGame.getWorld().getCurrentMap().getCollidableObjects();
		if(list.isEmpty()){
			return false;
		}
		CollisionMask shape = owner.getCollisionMask();
		/* checks all active entities on map */
		for(int objectID = 0; objectID < list.size(); objectID++){
			/* checks to see if collision shape intersects with entity collision shape */
			CollisionMask otherShape = list.get(objectID).getCollisionMask();
			if(shape.collidesWith(list.get(objectID))){
				Log.debug("The player entity has collided with something!");
				return true;
			}
		}
		return false;
	}
	
	public void enable(){
		enabled = true;
	}
	
	public void disable(){
		enabled = false;
	}
}
